/**
 * @file spi_ws2812.h
 * @author your name (you@domain.com)
 * @brief
 * @version 0.1
 * @date 2025-07-04
 *
 * @copyright Copyright (c) 2025
 *
 */
#ifndef SPI_WS2812_H
#define SPI_WS2812_H
#include "bflb_mtimer.h"
#include "bflb_spi.h"
#include "board.h"
#include "bflb_gpio.h"
#include <stdio.h>

#define SPI_MASTER_CASE 0
#define SPI_SLAVE_CASE 1
#define BITS_PER_LED_COLOR (sizeof(color_t) * 8)

// SPI数据为0的时序
#define SPI_NEO0 ((uint8_t)0b11000000)
// SPI数据为1的时序
#define SPI_NEO1 ((uint8_t)0b11111100)

#define DEFAULT_SPI_CLK GPIO_PIN_11
#define DEFAULT_SPI_MOSI GPIO_PIN_12
#define DEFAULT_SPI_MISO GPIO_PIN_5
#define DEFAULT_SPI_DIRVER "spi0"

typedef struct
{
	uint8_t r;
	uint8_t g;
	uint8_t b;
} color_t;

typedef struct
{
	float h; // 色相 (0-360)
	float s; // 饱和度 (0-1)
	float v; // 亮度 (0-1)
} hsv_color_t;

typedef struct
{
	uint16_t led_counts;			  // LED数量
	uint8_t *color_datas;			  // 24位颜色数据
	uint16_t color_data_size;		  // 颜色数据长度
	color_t *led_colors;			  // LED颜色
	bool inited;					  // 是否初始化，1表示已经初始化，0则表示未初始化
	struct bflb_device_s *sw2812_spi; // SPI设备
	uint32_t rainbow_offset;		  // 炫彩模式偏移量
} ws2812b_t;
void ws2812b_init(ws2812b_t *ws2812b, uint8_t ws2812_dat_pin);
void ws2812b_set_color(ws2812b_t *ws2812b, uint16_t led_index, color_t color);
void ws2812b_show(ws2812b_t *ws2812b);
void ws2812b_set_rainbow(ws2812b_t *ws2812b, uint8_t brightness);
void ws2812b_rainbow_cycle(ws2812b_t *ws2812b, uint8_t brightness, uint8_t wait_ms);
#endif // SPI_WS2812_H